package com.caiwangdai.app.ui.home.repay

import android.content.Intent
import android.os.Bundle
import com.caiwangdai.app.Const
import com.caiwangdai.app.R
import com.caiwangdai.app.bean.RepaymentB
import com.caiwangdai.app.origin.BaseFragment
import com.caiwangdai.app.origin.ToolBarManager
import com.caiwangdai.app.origin.list.delegator.RecyclerViewDelegator
import com.caiwangdai.app.ui.borrow.RepayOrderDetailActivity
import com.caiwangdai.app.ui.home.repay.list.BorrowAdapter
import com.caiwangdai.app.ui.home.repay.list.BorrowCommand

//还款
class BorrowRecordListFragment : BaseFragment() {
    override fun getLayoutId() = R.layout.fragment_repay


    companion object {
        val POSITION = "postion"
        val POSITION_ACTIVITY_SINGLE = 0//单个fragment在activity
        val POSITION_ACTIVITY_MULTIPLY = 1//多个fragment在activity
        val POSITION_VIEWPAGE = 2//viewPager中

        fun newInstance(position: Int): BorrowRecordListFragment {
            val intent = BorrowRecordListFragment()
            val bundle = Bundle()
            bundle.putInt(POSITION, position)
            intent.arguments = bundle
            return intent
        }
    }


    //view
    lateinit var mAdapter: BorrowAdapter
    var mListDelegator: RecyclerViewDelegator<RepaymentB>? = null
    lateinit var mCommand: BorrowCommand


    override fun initView() {
        val toolBar = ToolBarManager.with(mActivity, WMContent)
                .setTitle("借款记录")

        mCommand = BorrowCommand()
        mAdapter = BorrowAdapter(mActivity)
        mAdapter.onRepayListener = { orderId ->
            startActivityForResult(RepayOrderDetailActivity.newInstance(mActivity, orderId), Const.INTENT_REQUEST_ORDER_DETAIL)
        }
        mListDelegator = RecyclerViewDelegator(mActivity, WMContent, mAdapter, mCommand)
//        mListDelegator?.addHeadView(WMContent.inflate(R.layout.list_header_repay_single))
        mListDelegator?.setupEmptyView(tips = "暂无借款记录，请下拉刷新")
        mListDelegator?.changeToStatedyListView()

        //diff handle
        if (arguments?.getInt(POSITION) == POSITION_ACTIVITY_SINGLE) {
            mListDelegator?.beginHeaderRefreshing()
            toolBar.setNavigationIcon(R.drawable.ic_nav_arrow_white)
        }
    }


    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        //当在activity中此处不会被调用
        if (!hidden) mListDelegator?.beginHeaderRefreshing()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == Const.INTENT_REQUEST_ORDER_DETAIL) {
            mListDelegator?.beginHeaderRefreshing()
        }
    }


}